﻿namespace Home.Services.ProductServices
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    using System.Web;
    using System.Web.Http;
    using System.Web.OData;
    using System.Web.OData.Query;
    using System.Web.OData.Routing;
    using Home.Models;
    using Home.Repository;

    [OutputCache(Public = true, MaxAge = "00:30:00")]
    [ServiceHandleError(ServiceName = WebsiteServiceHelper.ProductODataServiceName)]
    public class ProductController : BaseODataController
    {
        private readonly ProductDbContext database = new ProductDbContext();

        #region Diamond operations

        [HttpGet]
        [EnableQuery(PageSize = 50)]
        [ODataRoute("Diamonds")]
        public IHttpActionResult GetDiamond(ODataQueryOptions<Diamond> options)
        {
            var diamonds = this.database.Diamonds.AsQueryable();
            return Ok(diamonds);
        }

        [HttpGet]
        [ODataRoute("Diamonds({id})")]
        public Task<IHttpActionResult> GetDiamond([FromODataUri] Guid id, ODataQueryOptions<Diamond> options)
        {
            var diamonds = this.database.Diamonds.Where(s => s.Id == id);
            return GetODataSingleAsync(diamonds, options);
        }

        [HttpGet]
        [ODataRoute("Diamonds({id})/Type")]
        [ODataRoute("Diamonds({id})/Shape")]
        [ODataRoute("Diamonds({id})/ReportType")]
        [ODataRoute("Diamonds({id})/ReportNumber")]
        [ODataRoute("Diamonds({id})/Caret")]
        [ODataRoute("Diamonds({id})/Clarity")]
        [ODataRoute("Diamonds({id})/Color")]
        [ODataRoute("Diamonds({id})/ColoredColor")]
        [ODataRoute("Diamonds({id})/Cut")]
        [ODataRoute("Diamonds({id})/Polish")]
        [ODataRoute("Diamonds({id})/Symmetry")]
        [ODataRoute("Diamonds({id})/Fluorescence")]
        [ODataRoute("Diamonds({id})/Comment")]
        [ODataRoute("Diamonds({id})/Status")]
        // TODO: remove the sale price if use price range
        [ODataRoute("Diamonds({id})/SalePrice")]
        public async Task<IHttpActionResult> GetDiamondProperty([FromODataUri] Guid id)
        {
            var diamond = await this.database.Diamonds.FindAsync(id);
            return GetODataProperty(diamond);
        }

        #endregion

        #region Helpers

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                this.database.Dispose();
            }

            base.Dispose(disposing);
        }

        #endregion
    }
}